﻿using System;
using System.Windows;

namespace Kaesoft.Hospedaje.Common
{
    #region Enumerados
    public enum MessageBoxButtons
    {
        Ok, YesNo, YesNoCancel, OkCancel
    }

    public enum MessageBoxIcon
    {
        Question, Information, Error, None, Warning
    }
    #endregion

    public class MessageWindowViewModel : ViewModelBase
    {
        #region Variables
        private string _icon;
        private bool _yesVisibility;
        private bool _noVisibility;
        private bool _cancelarVisibility;
        private string _textoYes;
        private string _mensaje;
        private RelayCommand _yesCommand;
        private RelayCommand _noCommand;
        private RelayCommand _cancelarCommand;
        #endregion

        #region Propiedades
        /// <summary>
        /// Path del ícono
        /// </summary>
        public string Icon
        {
            get
            {
                return _icon;
            }
            set
            {
                if (_icon != value)
                {
                    _icon = value;
                    OnPropertyChanged("Icon");
                }
            }
        }

        /// <summary>
        /// Es visible el botón yes?
        /// </summary>
        public bool YesVisibility
        {
            get
            {
                return _yesVisibility;
            }
            set
            {
                if (_yesVisibility != value)
                {
                    _yesVisibility = value;
                    OnPropertyChanged("YesVisibility");
                }
            }
        }

        /// <summary>
        /// Es visible el botón no?
        /// </summary>
        public bool NoVisibility
        {
            get
            {
                return _noVisibility;
            }
            set
            {
                if (_noVisibility != value)
                {
                    _noVisibility = value;
                    OnPropertyChanged("NoVisibility");
                }
            }
        }

        /// <summary>
        /// Es visible el botón cancelar?
        /// </summary>
        public bool CancelarVisibility
        {
            get
            {
                return _cancelarVisibility;
            }
            set
            {
                if (_cancelarVisibility != value)
                {
                    _cancelarVisibility = value;
                    OnPropertyChanged("CancelVisibility");
                }
            }
        }

        /// <summary>
        /// Texto para el boton Yes, Ok
        /// </summary>
        public string TextoYes
        {
            get
            {
                return _textoYes;
            }
            set
            {
                if (_textoYes != value)
                {
                    _textoYes = value;
                    OnPropertyChanged("TextoYes");
                }
            }
        }

        /// <summary>
        /// Resultado
        /// </summary>
        public MessageBoxResult Result { get; set; }

        /// <summary>
        /// Mensaje
        /// </summary>
        public string Mensaje
        {
            get
            {
                return _mensaje;
            }
            set
            {
                if (_mensaje != value)
                {
                    _mensaje = value;
                    OnPropertyChanged("Mensaje");
                }
            }
        }
        
        /// <summary>
        /// Yes command
        /// </summary>
        public RelayCommand YesCommand
        {
            get { return _yesCommand ?? (_yesCommand = new RelayCommand(ExecuteYes)); }
        }

        /// <summary>
        /// No command
        /// </summary>		
        public RelayCommand NoCommand
        {
            get { return _noCommand ?? (_noCommand = new RelayCommand(ExecuteNo)); }
        }

        /// <summary>
        /// Cancel command
        /// </summary>		
        public RelayCommand CancelarCommand
        {
            get { return _cancelarCommand ?? (_cancelarCommand = new RelayCommand(ExecuteCancel)); }
        }

        #endregion

        #region Eventos
        public event EventHandler<EventArgs<MessageBoxResult>> Closing;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public MessageWindowViewModel(string mensaje, MessageBoxButtons botones, MessageBoxIcon icono)
        {
            Mensaje = mensaje;
            DisplayButtons(botones);
            DisplayIcon(icono);
        }
        #endregion

        #region Metodos Privados
        /// <summary>
        /// Transformar enum a path de imagen
        /// </summary>
        /// <param name="icon">Icono</param>
        private void DisplayIcon(MessageBoxIcon icon)
        {
            switch (icon)
            {
                case MessageBoxIcon.Error:
                    Icon = "/Kaesoft.Hospedaje.Common;component/Images/error.png";
                    break;

                case MessageBoxIcon.Information:
                    Icon = "/Kaesoft.Hospedaje.Common;component/Images/info.png";
                    break;

                case MessageBoxIcon.Question:
                    Icon = "/Kaesoft.Hospedaje.Common;component/Images/question.png";
                    break;

                case MessageBoxIcon.Warning:
                    Icon = "/Kaesoft.Hospedaje.Common;component/Images/warning.png";
                    break;

                case MessageBoxIcon.None:
                    Icon = "/Kaesoft.Hospedaje.Common;component/Images/info.png";
                    break;
            }
        }

        /// <summary>
        /// Mostrar botones
        /// </summary>
        /// <param name="botones">Botones</param>
        private void DisplayButtons(MessageBoxButtons botones)
        {
            switch (botones)
            {
                case MessageBoxButtons.Ok:
                    CancelarVisibility = false;
                    NoVisibility = false;
                    YesVisibility = true;
                    TextoYes = "Aceptar";
                    break;

                case MessageBoxButtons.OkCancel:
                    CancelarVisibility = true;
                    NoVisibility = false;
                    YesVisibility = true;
                    TextoYes = "Aceptar";
                    break;

                case MessageBoxButtons.YesNo:
                    CancelarVisibility = false;
                    NoVisibility = true;
                    YesVisibility = true;
                    TextoYes = "Sí";
                    break;

                case MessageBoxButtons.YesNoCancel:
                    CancelarVisibility = true;
                    NoVisibility = true;
                    YesVisibility = true;
                    TextoYes = "Sí";
                    break;
            }
        }

        /// <summary>
        /// Sí, Ok
        /// </summary>
        /// <param name="param"></param>
        private void ExecuteYes(object param)
        {
            Enabled = false;
            //If user presses Yes button (may be Ok)
            Result = TextoYes.Equals("Sí") ? MessageBoxResult.Yes : MessageBoxResult.OK;                
            CloseWindow();
        }

        /// <summary>
        /// No
        /// </summary>
        /// <param name="param"></param>
        private void ExecuteNo(object param)
        {
            Enabled = false;
            Result = MessageBoxResult.No;
            CloseWindow();
        }

        /// <summary>
        /// Cancelar
        /// </summary>
        /// <param name="param"></param>
        private void ExecuteCancel(object param)
        {
            Enabled = false;
            Result = MessageBoxResult.Cancel;
            CloseWindow();
        }

        /// <summary>
        /// Close the window
        /// </summary>
        private void CloseWindow()
        {
            if (Closing != null)
                Closing(this, new EventArgs<MessageBoxResult>(Result));
        }
        #endregion
    }
}